﻿using System;
using System.Collections.Generic;
using DG.Tweening;
using Gp.Scripts.Equip;
using Sirenix.OdinInspector;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Gp.Scripts.Core {
    public class UnitWeaponModel : MonoBehaviour {
        public WeaponTypeCode code;

        [SerializeField] protected Animator animator;

        private CombatantWeaponPivot Pivot { get; set; }
        protected WeaponEntity WeaponEntity;
        public virtual IEnumerable<GameObject> Particles => particles;
        public virtual WeaponTrailEffect[] Trails => trails;


        [LabelText("远程武器投射物")] public WeaponProjectileFx projectile;
        

        [SerializeField] protected GameObject[] particles;
        
        public WeaponTrailEffect[] trails;
        
        
        public void Init(CombatantWeaponPivot weaponPivot, WeaponEntity weapon) {
            Pivot = weaponPivot;
            WeaponEntity = weapon;
        }
        
        private void Start() {
            if (animator == null) animator = GetComponent<Animator>();
        }

        


        private static readonly int Selecting = Animator.StringToHash("Selecting");
        
        
        public void SetSelectingTrigger() {
            if (!animator) return;
            animator.SetBool(Selecting, true);
        }
        
        public void UnSetSelectingTrigger() {
            if (!animator) return;
            animator.SetBool(Selecting, false);
        }
        
        
        
        
        
        public void ActivateTrail() {
            foreach (var trail in trails) {
                trail.ActivateTrail();
            }
        }

        public void DeactivateTrail() {
            foreach (var trail in trails) {
                trail.DeactivateTrail();
            }
        }
        
        
        
        
        
        
        
        public void OnAttackHitTrigger(BaseUnit target) {
            if (projectile == null) return;
            var obj = Instantiate(projectile);
            obj.Shoot(transform.position, target.transform.position);
        }

        public virtual void OnUnitModelAnimationEvent(string eventName) {
            if (eventName == UnitAnimEventType.TrailEffectActivate.EventName()) {
                ActivateTrail();  
            } else if (eventName == UnitAnimEventType.TrailEffectDeactivate.EventName()) {
                DeactivateTrail();
            }
        }
    }
}